<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span='8'>
          <el-input placeholder="请输入内容" v-model='queryInfo.query'>
            <el-button slot="append" icon="el-icon-search" @click='getOrderList'></el-button>
          </el-input>
        </el-col>
      </el-row>

      <el-table :data='orderlist' border stripe style="margin-top:20px">
        <el-table-column type='index' label='#'></el-table-column>
        <el-table-column label='订单编号' prop='order_number'></el-table-column>
        <el-table-column label='订单价格' prop='order_price'></el-table-column>
        <el-table-column label='是否发货'>
          <template v-slot='{row}'>
            <el-tag type="danger" v-if="row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label='是否付款' prop='is_send'></el-table-column>
        <el-table-column label='下单时间'>
          <template v-slot='{row}'>{{row.update_time | formatDate}}</template>
        </el-table-column>
        <el-table-column label='操作'>
          <template>
            <el-button icon='el-icon-edit' type='primary' circle @click='orderDialogVisible=true'></el-button>
            <el-button icon='el-icon-location-information' type='success' circle @click='kuaiShow'>
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[10, 20, 30, 40]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" background :total="total">
      </el-pagination>

      <el-dialog title="修改地址" :visible.sync="orderDialogVisible" width="50%" @close='addressClose'>
        <el-form :model='orderForm' :rules='orderFormRules' ref='orderFormRef'>
          <el-form-item label='省市区/县' prop='address1'>
            <el-cascader :options="cityData" v-model="orderForm.address1" :props="{ expandTrigger: 'hover' }">
            </el-cascader>
          </el-form-item>
          <el-form-item label='详细地址' prop='address2'>
            <el-input v-model='orderForm.address2'></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="orderDialogVisible = false" style="margin-right:20px">取 消</el-button>
          <el-button type="primary" @click="orderDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

      <el-dialog title="物流进度" :visible.sync="logisticsDialogVisible" width="50%">
        <el-timeline :reverse="reverse">
          <el-timeline-item v-for="(activity, index) in activities" :key="index" :timestamp="activity.ftime">
            {{activity.context}}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>

<script scoped>
import { kuaiAPI } from '@/api/kuai'
import cityData from './citydata'
import { OrderAPI } from '@/api/order'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: {},
  // 组件状态值
  data () {
    return {
      reverse: true,
      activities: [],
      logisticsDialogVisible: false,
      orderForm: {
        address1: [],
        address2: ''
      },
      orderFormRules: {
        address1: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        address2: [{ required: true, message: '请输入地址', trigger: 'blur' }]
      },
      cityData,
      orderDialogVisible: false,
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      orderlist: []
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    async kuaiShow () {
      const { data: res } = await kuaiAPI()
      this.activities = res.data
      this.logisticsDialogVisible = true
    },
    addressClose () {
      this.$refs.orderFormRef.resetFields()
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    async getOrderList () {
      const { data: res } = await OrderAPI(this.queryInfo)
      if (res.meta.status !== 200) {
        this.$message({
          message: '获取订单数据列表失败！',
          type: 'error'
        })
      }
      this.total = res.data.total
      this.orderlist = res.data.goods
    }
  },
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.getOrderList()
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  }
}
</script>

<style scoped>
.el-cascader {
  width: 100%;
}
</style>
